package com.zhl.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 创建、更新时间处理器
 * @author zhanghaoliang
 * @since 2021/1/12 11:31
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private LocalDateTime now = LocalDateTime.now();
    private String userName = "zhanghaoliang";

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill.....");
        this.setFieldValByName("gmtCreate",now, metaObject);
        this.setFieldValByName("createBy",userName,metaObject);
        this.setFieldValByName("gmtModified",now,metaObject);
        this.setFieldValByName("updateBy",userName,metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill.....");
        this.setFieldValByName("gmtModified",now,metaObject);
        this.setFieldValByName("updateBy",userName,metaObject);
    }
}
